/**************************************************************************************
*
*	Copyright (C) 2009 - 2012 Brent Andrew O. Chua - See LICENSE.txt for more info.
*		
*	Filename	-	VertexSource.cpp
*
*	Comments	-	See VertexSource.h
*
**************************************************************************************/
#include "../Include/VertexSource.h"


namespace Pulse
{
	VertexSource::VertexSource( void )
	{
		m_elementType = EVertexElement::UNKNOWN;
		m_elementSize = 0;
		m_elementCount = 0;
		//m_writeMarker = 0;
		m_pBuffer = PSX_NULL;
	}

	VertexSource::VertexSource( EVertexElement::Type type, SIZE_T32 count )
	{
		m_elementType = type;
		m_elementSize = VertexElementSizes::GetSize( type );
		m_elementCount = 0;
		//m_writeMarker = 0;
		m_pBuffer = PSX_NULL;

		ResizeBuffer( count );
	}

	VertexSource::~VertexSource( void )
	{
		ClearBuffer();
	}

	EErrorCode::Type VertexSource::Initialize( EVertexElement::Type type, SIZE_T32 count )
	{
		m_elementType = type;
		m_elementSize = VertexElementSizes::GetSize( type );
		m_elementCount = 0;
		//m_writeMarker = 0;
		m_pBuffer = PSX_NULL;

		return ResizeBuffer( count );
	}

	//UPOS_T32 VertexSource::AddPosition( Vector3 *pPos )
	//{
	//	if ( CheckCanAdd( EVertexElement::POSITION ))
	//	{
	//		m_pPositions[m_writeMarker] = *pPos;
	//		++m_writeMarker;
	//	}

	//	return m_writeMarker;
	//}

	//UPOS_T32 VertexSource::AddPositions( SIZE_T32 numElements, Vector3 *pPos )
	//{
	//	if ( CheckCanAdd( EVertexElement::POSITION, numElements ))
	//	{
	//		PSX_MemCopyPerByte( &m_pPositions[m_writeMarker], pPos, m_elementSize * numElements );
	//		m_writeMarker += numElements;
	//	}

	//	return m_writeMarker;
	//}

	//UPOS_T32 VertexSource::AddNormal( FLOAT x, FLOAT y, FLOAT z )
	//{
	//	if ( CheckCanAdd( EVertexElement::NORMAL ) )
	//	{
	//		m_pNormals[m_writeMarker].x = x;
	//		m_pNormals[m_writeMarker].y = y;
	//		m_pNormals[m_writeMarker].z = z;
	//		++m_writeMarker;
	//	}

	//	return m_writeMarker;
	//}

	//UPOS_T32 VertexSource::AddNormal( Vector3 *pNormal )
	//{
	//	return AddNormal( pNormal->x, pNormal->y, pNormal->z );
	//}

	//UPOS_T32 VertexSource::AddTextureDiffuse( FLOAT x, FLOAT y )
	//{
	//	if ( CheckCanAdd( EVertexElement::TEXTURE_DIFFUSE ) )
	//	{
	//		m_pTextureDiffuse[m_writeMarker].x = x;
	//		m_pTextureDiffuse[m_writeMarker].y = y;
	//		++m_writeMarker;
	//	}

	//	return m_writeMarker;
	//}

	//UPOS_T32 VertexSource::AddTextureDiffuse( Vector2 *pUVDiffuse )
	//{
	//	return AddTextureDiffuse( pUVDiffuse->x, pUVDiffuse->y );
	//}

	//BOOL VertexSource::CheckCanAdd( EVertexElement::Type typeToAdd, SIZE_T32 numToAdd )
	//{
	//	if ( m_writeMarker + numToAdd > m_elementCount )
	//	{
	//		PSX_PushError( "Can not add more elements. Elements stored reached maximum count." );
	//		return FALSE;
	//	}

	//	if ( m_elementType != typeToAdd )
	//	{
	//		PSX_PushError( "Vertex source type mismatch element type to add." );
	//		return FALSE;
	//	}

	//	return TRUE;
	//}

	void VertexSource::ClearBuffer( void )
	{
		PSX_SafeDeleteArray( m_pBuffer );
	}

	EErrorCode::Type VertexSource::ResizeBuffer( UINT count )
	{
		ClearBuffer();

		BYTE *pNewBuffer = new BYTE[count * m_elementSize];

		if ( pNewBuffer == PSX_NULL )
			return EErrorCode::MEMORY;

		//if ( m_writeMarker )
		//{
		//	UINT copyCount = m_writeMarker > count ? count : m_writeMarker;
		//	PSX_MemCopyPerByte( pNewBuffer, m_pBuffer, m_elementSize * copyCount );
		//	m_writeMarker = copyCount;
		//}

		ClearBuffer();
		m_pBuffer = pNewBuffer;
		m_elementCount = count;

		return EErrorCode::OKAY;
	}

}